//
//  ViewController.m
//  22-QQMusic
//
//  Created by Snow on 16/8/12.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "AFURLSessionManager.h"
#import "AFURLRequestSerialization.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "NetWorking.h"
#import "MyCollectionViewCell.h"
#import "ModelOfCell.h"
#import "MyCollectionReusableView.h"
#import "GXCycleImage.h"
#import "CollectionReusableView.h"
#import "ModelOfSection.h"
#import "UIButton+WebCache.h"
#import "CollectionViewCellOfThird.h"
#import "CollectionViewCellOfFour.h"
#import "SecondViewController.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
//分区头部标题
@property (nonatomic, strong) NSMutableArray *arrayOfSectionTitle;
//轮播图
@property (nonatomic, strong) NSMutableArray *arrayOfScrollView;
//头部视图
@property (nonatomic, strong) NSMutableArray *arrayOfSectionView;
//cell视图
@property (nonatomic, strong) NSMutableArray *arrayOfCellView;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"音乐";
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self handleData];
    [self createCollectionView];
}
- (void)handleData
{
    _arrayOfScrollView = [NSMutableArray array];
    _arrayOfSectionView = [NSMutableArray array];
    _arrayOfSectionTitle =  [NSMutableArray array];
    _arrayOfCellView =  [NSMutableArray array];
    
    [NetWorking GETWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400" parameters:nil success:^(id responseObject) {
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *dict in arr) {
            ModelOfSection *modelOfSection = [[ModelOfSection alloc] init];
            [modelOfSection setValuesForKeysWithDictionary:dict];
            [_arrayOfSectionView addObject:modelOfSection];
        }
        //collectionview刷新数据
        [_collectionView reloadData];
    
} filed:^(NSError *error) {
    NSLog(@"请求失败: %@", error);
}];
}
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 15) / 2, 150);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
//    _collectionView.backgroundColor = [UIColor clearColor];
   [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[CollectionViewCellOfThird class] forCellWithReuseIdentifier:@"cell1"];
    [_collectionView registerClass:[CollectionViewCellOfFour class] forCellWithReuseIdentifier:@"cell2"];
    [_collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse"];
    [_collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _arrayOfSectionView.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return 0;
    } else {
        ModelOfSection *model = [_arrayOfSectionView objectAtIndex:section];
        return model.data.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        CollectionViewCellOfThird *cellOfTwo = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        ModelOfSection *model = [_arrayOfSectionView objectAtIndex:indexPath.section];
        NSArray *array = model.data;
        NSMutableArray *arrOfCell2 = [NSMutableArray array];
        for (NSDictionary *diction in array) {
            ModelOfCell *model = [[ModelOfCell alloc] init];
            [model setValuesForKeysWithDictionary:diction];
            [arrOfCell2 addObject:model];
        }
        ModelOfCell *modelOfCell = [arrOfCell2 objectAtIndex:indexPath.row];
        cellOfTwo.labelOfTitle.text = modelOfCell.name;
        cellOfTwo.labelOfTitle.textColor = [UIColor whiteColor];
        cellOfTwo.labelOfContent.textColor = [UIColor whiteColor];
        cellOfTwo.labelOfContent.text = modelOfCell.desc;
        cellOfTwo.labelOfTitle.font = [UIFont systemFontOfSize:12];
        cellOfTwo.labelOfContent.font = [UIFont systemFontOfSize:10];
        return cellOfTwo;
    } else if (indexPath.section == 7) {
        CollectionViewCellOfFour *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        cell2.backgroundColor = [UIColor colorWithRed:0.071 green:0.118 blue:0.180 alpha:1];
        ModelOfSection *model = [_arrayOfSectionView objectAtIndex:indexPath.section];
        NSArray *array = model.data;
        NSMutableArray *arrOfCell = [NSMutableArray array];
        for (NSDictionary *diction in array) {
            ModelOfCell *model = [[ModelOfCell alloc] init];
            [model setValuesForKeysWithDictionary:diction];
            [arrOfCell addObject:model];
        }
        ModelOfCell *modelOfCell = [arrOfCell objectAtIndex:indexPath.row];
        cell2.labelOfTitle.text = modelOfCell.name;
        cell2.labelOfTitle.font = [UIFont systemFontOfSize:12];
        cell2.labelOfTitle.textColor = [UIColor whiteColor];
        cell2.labelOfContent.text = modelOfCell.desc;
        cell2.labelOfContent.font = [UIFont systemFontOfSize:10];
        cell2.labelOfContent.textColor = [UIColor whiteColor];
        [cell2.image sd_setImageWithURL:[NSURL URLWithString:modelOfCell.picUrl]];
        return cell2;
    } else {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ModelOfSection *model = [_arrayOfSectionView objectAtIndex:indexPath.section];
    NSArray *array = model.data;
    NSMutableArray *arrOfCell = [NSMutableArray array];
    for (NSDictionary *diction in array) {
        ModelOfCell *model = [[ModelOfCell alloc] init];
        [model setValuesForKeysWithDictionary:diction];
        [arrOfCell addObject:model];
    }
    ModelOfCell *modelOfCell = [arrOfCell objectAtIndex:indexPath.row];
    cell.labelOfTitle.text = modelOfCell.desc;
        cell.labelOfTitle.font = [UIFont systemFontOfSize:12];
    cell.labelOfTitle.textColor = [UIColor whiteColor];
    cell.labelOfContent.text = modelOfCell.name;
        cell.labelOfContent.font = [UIFont systemFontOfSize:10];
    cell.labelOfContent.textColor = [UIColor whiteColor];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:modelOfCell.picUrl]];
    return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        CollectionReusableView *reuseimage = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        reuseimage.backgroundColor = [UIColor clearColor];
        ModelOfSection *model = [_arrayOfSectionView objectAtIndex:indexPath.section];
        NSArray *array = model.data;
        NSMutableArray *arrOfCell = [NSMutableArray array];
        for (NSDictionary *diction in array) {
            ModelOfCell *modelOfCell = [[ModelOfCell alloc] init];
            [modelOfCell setValuesForKeysWithDictionary:diction];
            [arrOfCell addObject:modelOfCell.picUrl];
        }
        GXCycleImage *image = [[GXCycleImage alloc] initWithFrame:CGRectMake(0, 0, reuseimage.frame.size.width, reuseimage.frame.size.height)];
        image.arrayImage = arrOfCell;
        [reuseimage addSubview:image];
        return reuseimage;
    } else if (indexPath.section == 1)
    {
        CollectionReusableView *collect = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1" forIndexPath:indexPath];
        collect.backgroundColor = [UIColor clearColor];
        ModelOfSection *model = [_arrayOfSectionView objectAtIndex:indexPath.section];
        NSArray *array = model.data;
        NSMutableArray *arrOfCell = [NSMutableArray array];
        for (NSDictionary *diction in array) {
            ModelOfCell *modelOfCell = [[ModelOfCell alloc] init];
            [modelOfCell setValuesForKeysWithDictionary:diction];
            [arrOfCell addObject:modelOfCell];
        }
        for (int i = 0; i < array.count; i++) {
            ModelOfCell *modelOfView = arrOfCell[i];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(collect.frame.size.width / array.count * i + 40, 0, collect.frame.size.width / array.count - 80, 20);
            [button sd_setImageWithURL:[NSURL URLWithString:modelOfView.picUrl] forState:UIControlStateNormal];
            [collect addSubview:button];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(collect.frame.size.width / array.count * i, 20, collect.frame.size.width / array.count, 20)];
            label.text = modelOfView.name;
            label.textAlignment = 1;
            label.font = [UIFont systemFontOfSize:10];
            label.textColor = [UIColor whiteColor];
            [collect addSubview:label];
        }
        return collect;
    
    } else {
        MyCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuse" forIndexPath:indexPath];
        ModelOfSection *model = [_arrayOfSectionView objectAtIndex:indexPath.section];
        reuseView.labelOfTitle.text = model.name;
        reuseView.labelOfTitle.textAlignment = 1;
        reuseView.labelOfTitle.textColor = [UIColor whiteColor];
        return reuseView;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        return CGSizeMake((self.view.frame.size.width - 15)/ 2, 70);
    } else if (indexPath.section == 7) {
        return CGSizeMake(self.view.frame.size.width - 10, 60);
    }else {
        return CGSizeMake((self.view.frame.size.width - 20)/ 3, 100);
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(self.view.frame.size.width, 160);
    } else if (section == 1) {
        return CGSizeMake(self.view.frame.size.width, 40);
    } else {
        return CGSizeMake(self.view.frame.size.width, 30);
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SecondViewController *second = [[SecondViewController alloc] init];
    ModelOfSection *model = [_arrayOfSectionView objectAtIndex:indexPath.section];
    ModelOfCell *cell = [[ModelOfCell alloc] init];
    [cell setValuesForKeysWithDictionary:[model.data objectAtIndex:indexPath.row]];
    second.stringOfValue = [cell.action objectForKey:@"value"];
    [self.navigationController pushViewController:second animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
